package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 17/10/12
 * Time: 10:19
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocols/Scorecard")
public class ScorecardController extends ActionSupport {

    private ProtocolCRUDService protocolCRUDService;
    private ScorecardConfigurationService scorecardConfigurationService;
    private RoleCRUDService roleCRUDService;
    private ProtocoloCompromisopaisService protocoloCompromisopaisService;

    private List<Protocolscorecard> protocolscorecardList = new ArrayList<Protocolscorecard>();
    private List<Role> roleList = new ArrayList<Role>();
    private ProtocoloCompromisopais compromisopais;
    private GlobalCommitmentService globalCommitmentService;
    private GlobalCommitment globalCommitment;

    private String mensaje;

    @Autowired
    public ScorecardController(ProtocolCRUDService protocolCRUDService, ScorecardConfigurationService scorecardConfigurationService,
                               RoleCRUDService roleCRUDService, ProtocoloCompromisopaisService protocoloCompromisopaisService,
                               GlobalCommitmentService globalCommitmentService) {
        this.protocolCRUDService = protocolCRUDService;
        this.scorecardConfigurationService = scorecardConfigurationService;
        this.roleCRUDService = roleCRUDService;
        this.protocoloCompromisopaisService = protocoloCompromisopaisService;
        this.globalCommitmentService = globalCommitmentService;
    }

    @Action(value="main", results={
            @Result(name="success",location="/protocols.scorecard.tiles", type="tiles")
    })
    public String execute() {

        ProtocoloCompromisopais aux = null;
        Protocolo protocolo = null;
        try {
            aux = protocolCRUDService.recoverProtocoloCompromisopais();
            protocolo = protocolCRUDService.recoverProtocolo();
        } catch (NoProtocoloCompromisopaisSelectedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if(aux == null)
            return null;
        roleList = roleCRUDService.findStaffRole();
        compromisopais = aux;

        globalCommitment = globalCommitmentService.findByProtocoloId(protocolo.getId());

        if(  aux.getScorecard() == null ){

            protocolscorecardList = new ArrayList<Protocolscorecard>();
            Integer compromisopaisId = aux.getCompromisopais().getId();

            List<ScorecardConfiguration> configurationList = scorecardConfigurationService.findByCompromisopaisId(compromisopaisId);


            for(ScorecardConfiguration configuration : configurationList){
                Protocolscorecard protocolscorecard = new Protocolscorecard();
                protocolscorecard.setMilestone(configuration.getDescription());
                protocolscorecard.setPredecessor( configuration.getPredecessor());
                protocolscorecard.setScorecardOrder( configuration.getScorecardOrder());
                protocolscorecard.setMetrics(configuration.getMetrics());
                protocolscorecard.setDuration(configuration.getDuration());
                protocolscorecard.setScid(configuration.getId());
                if( configuration.getStatus() == null  )
                       protocolscorecardList.add(protocolscorecard);
                else{
                    if( configuration.getStatus().toUpperCase().equals("CHRONIC") && protocolo.getChronic().toUpperCase().equals("YES")  )
                        protocolscorecardList.add(protocolscorecard);
                    if( configuration.getStatus().toUpperCase().equals("NON CHRONIC") && protocolo.getChronic().toUpperCase().equals("NO")  )
                        protocolscorecardList.add(protocolscorecard);
                }
            }

        } else{
            protocolscorecardList = new ArrayList<Protocolscorecard>(aux.getScorecard().getProtocolscorecards());
        }

        Collections.sort(protocolscorecardList, new CustomComparator());

        return SUCCESS;
    }

    public class CustomComparator implements Comparator<Protocolscorecard> {
        @Override
        public int compare(Protocolscorecard o1, Protocolscorecard o2) {
            return o1.getScorecardOrder().compareTo(o2.getScorecardOrder());
        }
    }


    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction", params = {"mensaje", "Changes were saved"})
    })
    public String save() {

        ProtocoloCompromisopais protocoloCompromisopais = null;
        try {
            protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
        } catch (NoProtocoloCompromisopaisSelectedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        try{
            Scorecard scorecard = new Scorecard();
            for(Protocolscorecard protocolscorecard: protocolscorecardList){
                if( protocolscorecard.getDuration() == null)
                    protocolscorecard.setDuration(0);
                protocolscorecard.setScorecardConfigurationid( scorecardConfigurationService.findById(protocolscorecard.getScorecardConfigurationid().getId()) );

                protocolscorecard.setScorecard(scorecard);
            }
            Set<Protocolscorecard> set = new HashSet<Protocolscorecard>(protocolscorecardList);
            scorecard.setProtocolscorecards(set);

        protocoloCompromisopaisService.saveScorecard(protocoloCompromisopais,scorecard);


        }catch (Exception e){
            System.out.println(e.toString());
        }



        return SUCCESS;
    }




    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    public List<Protocolscorecard> getProtocolscorecardList() {
        return protocolscorecardList;
    }

    public void setProtocolscorecardList(List<Protocolscorecard> protocolscorecardList) {
        this.protocolscorecardList = protocolscorecardList;
    }

    public String getMensaje() {
        return mensaje;
    }

    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

    public ProtocoloCompromisopais getCompromisopais() {
        return compromisopais;
    }

    public void setCompromisopais(ProtocoloCompromisopais compromisopais) {
        this.compromisopais = compromisopais;
    }

    public GlobalCommitment getGlobalCommitment() {
        return globalCommitment;
    }

    public void setGlobalCommitment(GlobalCommitment globalCommitment) {
        this.globalCommitment = globalCommitment;
    }
}
